package 寒假作业.NC;

public class DeleteReverse {
    public class ListNode {
        int val;
        ListNode next = null;

        ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode deleteDuplication(ListNode pHead) {
        if(pHead == null){
            return pHead;
        }
        ListNode prev = null;
        ListNode node = pHead;
        ListNode nextNode = node.next;
        while (nextNode != null){
            if (node.val != nextNode.val){
                prev = node;
                node = nextNode;
                nextNode = nextNode.next;
            }else {
                while (nextNode != null && nextNode.val == node.val){
                    nextNode = nextNode.next;
                }
                if (prev == null){
                    pHead = nextNode;
                }else {
                    prev.next = nextNode;
                }
                while (node != nextNode){
                    ListNode p = node;
                    node = node.next;
                }
                if (nextNode != null){
                    nextNode = nextNode.next;
                }
            }
        }
        return pHead;
    }
}
